package com.example.cursor.controller;

import com.example.cursor.common.Result;
import com.example.cursor.entity.Product;
import com.example.cursor.entity.SeckillRecord;
import com.example.cursor.entity.User;
import com.example.cursor.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @GetMapping("/products/{productId}")
    public Result<Product> getProduct(@PathVariable Long productId) {
        return Result.success(seckillService.getProduct(productId));
    }

    @PostMapping("/products/{productId}")
    public Result<SeckillRecord> seckill(
            @PathVariable Long productId,
            HttpSession session) {
        // 从session获取用户信息
        User user = (User) session.getAttribute("user");
        return Result.success(seckillService.doSeckill(user.getId(), productId));
    }
}